package com.smz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smz.dao.TCargoDAO;
import com.smz.dao.TIoWarehouseDAO;
import com.smz.entity.TCargo;
import com.smz.entity.TIoWarehouse;
import com.smz.service.TIoWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TIoWarehouseServiceImpl extends ServiceImpl<TIoWarehouseDAO, TIoWarehouse> implements TIoWarehouseService {

    @Autowired
    private TIoWarehouseDAO tIoWarehouseDAO;
    @Autowired
    private TIoWarehouseService tIoWarehouseService;
    @Autowired
    private TCargoDAO tCargoDAO;


    @Override
    public IPage<TIoWarehouse> findPageTIoWareHouse(int current, int size) {
        return tIoWarehouseDAO.selectPageTIoWareHouse(new Page( current, size));
    }

    @Override
    @Transactional
    public void addTIoWareHouses(TIoWarehouse tIoWarehouse) {
        //根据商品名查询货位对应数据
        TCargo tCargo = tCargoDAO.selectOne(new QueryWrapper<TCargo>().lambda().eq(TCargo::getProduct, tIoWarehouse.getPName()).eq(TCargo::getBatch,tIoWarehouse.getBatch()));
        if (tCargo!=null){
            //插入出入库表数据
            tIoWarehouseService.save(tIoWarehouse);
            //出库减去货位数量
            int num = tCargo.getNum()-tIoWarehouse.getPNum();
            tCargo.setNum(num);
            //更新货位数量
            tCargoDAO.updateById(tCargo);
        }else {
            return;
        }


    }

    @Override
    public IPage<TIoWarehouse> findPageByNumberAndName(String orderNumber, String lot, int size, int current) {
        return tIoWarehouseDAO.selectPageByNumberAndName(new Page(current,size),orderNumber,lot);
    }
}
